//
//  TicketInfoView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/10.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol TicketInfoViewDelegate {
    // 订单删除
    func ticket_delete(orderid:String)
    // 订单完成
    func ticket_finish(orderid:String)
}

class TicketInfoView: UIView {
    
    public var orderInfo: OrderInfo? {
        didSet {
            setNeedsLayout()
        }
    }
    
    public var account:OrderAccount? {
        didSet {
            setNeedsLayout()
        }
    }
    // 滑动
    private var scrollview = UIScrollView()
    // 回调
    public var delegate:TicketInfoViewDelegate?
    
    override func layoutSubviews() {
        // 头部
        let subTopView = SubTopView(frame: CGRect(x: 0, y: 0, width: frame.width, height: MARGIN100))
        subTopView.subTopOrderInfo = orderInfo
        subTopView.backgroundColor = UIColor.white
        scrollview.addSubview(subTopView)
        // 机票信息
        let subTicInfoViewY = subTopView.frame.maxY + MARGIN10
        let subTicInfoView = SubTicketInfoView(frame: CGRect(x: 0, y: subTicInfoViewY, width: frame.width, height: 160))
        subTicInfoView.backgroundColor = UIColor.white
        subTicInfoView.subTicketInfoOrderInfo = orderInfo
        scrollview.addSubview(subTicInfoView)
        // 乘机人
        let subPassengerY = subTicInfoView.frame.maxY
        let subPassengertView = SubPassengerView()
        subPassengertView.frame = CGRect(x: 0, y: subPassengerY, width: frame.width, height: MARGIN100)
        subPassengertView.subUser = account
        subPassengertView.backgroundColor = UIColor.white
        scrollview.addSubview(subPassengertView)
        // 联系人
        let subContactY = subPassengertView.frame.maxY + MARGIN10
        let subContactView = SubContactView(frame: CGRect(x: 0, y: subContactY, width: frame.width, height: MARGIN100))
        subContactView.subPassenger = account
        subContactView.backgroundColor = UIColor.white
        scrollview.addSubview(subContactView)
        // 座位号
        let searnumberY = subContactView.frame.maxY + MARGIN10
        let searnumberView = SubSeatNumberView(frame: CGRect(x: 0, y: searnumberY, width: frame.width, height: MARGIN50))
        searnumberView.subOrderInfo = orderInfo
        searnumberView.backgroundColor = UIColor.white
        scrollview.addSubview(searnumberView)
        // 订单时间
        let subOrderY = searnumberView.frame.maxY + MARGIN10
        let subOrderView = SubOrderView(frame: CGRect(x: 0, y: subOrderY, width: frame.width, height: MARGIN140))
        subOrderView.subOrderInfo = orderInfo
        subOrderView.backgroundColor = UIColor.white
        scrollview.addSubview(subOrderView)
        // 订单 删除按钮
        let deleteLabel = UILabel(title: "删除", color: COLOR_RED, size: 12, align: .center)
        deleteLabel.layer.borderWidth = 1
        deleteLabel.layer.cornerRadius = 3
        deleteLabel.layer.borderColor = COLOR_RED.cgColor
        let deleteLabelX = frame.width - MARGIN70 - MARGIN
        let deleteLabelY = subOrderView.frame.maxY + MARGIN20
        deleteLabel.frame = CGRect(x: deleteLabelX, y: deleteLabelY, width: MARGIN70, height: MARGIN35)
        // 订单 删除按钮 添加点击事件
        deleteLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickDelete)))
        // 订单 完成按钮
        let finishLabel = UILabel(title: "已完成", color: UIColor.gray, size: 12, align: .center)
        finishLabel.layer.borderWidth = 1
        finishLabel.layer.cornerRadius = 3
        finishLabel.layer.borderColor = UIColor.gray.cgColor
        let labelX = frame.width - MARGIN140 - MARGIN * 2
        finishLabel.frame = CGRect(x: labelX, y: deleteLabelY, width: MARGIN70, height: MARGIN35)
        // 订单 完成按钮 添加点击事件
        finishLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickFinish)))
        if orderInfo?.orderstatus == 0 {
            scrollview.addSubview(deleteLabel)
            scrollview.addSubview(finishLabel)
        }
        if orderInfo?.orderstatus == 1 {
            scrollview.addSubview(deleteLabel)
        }
        // 设置scrollvew属性
        scrollview.showsVerticalScrollIndicator = false
        scrollview.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollview.contentSize = CGSize(width: frame.width, height: finishLabel.frame.maxY + MARGIN20)
        addSubview(scrollview)
    }
    
    @objc func clickFinish() -> Void {
        if let orderid = orderInfo?.orderid{
            delegate?.ticket_finish(orderid: orderid)
        }
    }
    
    @objc func clickDelete() -> Void {
        if let orderid = orderInfo?.orderid{
            delegate?.ticket_delete(orderid: orderid)
        }
    }
    
    // 订单时间
    class SubOrderView: UIView {
        
        public var subOrderInfo: OrderInfo?
        
        override func layoutSubviews() {
            guard let ticket = subOrderInfo else {
                return
            }
            // 创建时间文本
            let createView = UILabel(title: "创建时间", color: COLOR_NICE, size: 15)
            createView.frame = CGRect(x: MARGIN, y: MARGIN10, width: MARGIN80, height: MARGIN40)
            addSubview(createView)
            // 创建时间
            let createViewW = frame.width - createView.frame.maxX - MARGIN
            let createTimeView = UILabel(title: "", color: UIColor.gray, size: 14,align: .right)
            createTimeView.text = ticket.createtime
            createTimeView.frame = CGRect(x: createView.frame.maxX, y: MARGIN10, width: createViewW, height: MARGIN40)
            addSubview(createTimeView)
            // 更新时间文本
            let updateTimeY = createTimeView.frame.maxY
            let updateView = UILabel(title: "更新时间", color: COLOR_NICE, size: 15)
            updateView.frame = CGRect(x: MARGIN, y: updateTimeY, width: MARGIN100, height: MARGIN40)
            addSubview(updateView)
            // 更新时间
            let updateTimeView = UILabel(title: "", color: UIColor.gray, size: 14,align: .right)
            updateTimeView.text = ticket.updatetime
            updateTimeView.frame = CGRect(x: createView.frame.maxX, y: updateTimeY, width: createViewW, height: MARGIN40)
            addSubview(updateTimeView)
            // 订单编号文本
            let orderView = UILabel(title: "订单编号", color: COLOR_NICE, size: 15)
            let orderViewY = updateTimeView.frame.maxY
            orderView.frame = CGRect(x: MARGIN, y: orderViewY, width: MARGIN80, height: MARGIN40)
            addSubview(orderView)
            // 订单编号
            let orderViewLabel = UILabel(title: "", color: UIColor.gray, size: 14,align: .right)
            orderViewLabel.text = ticket.orderid
            orderViewLabel.frame = CGRect(x: createView.frame.maxX, y: orderViewY, width: createViewW, height: MARGIN40)
            addSubview(orderViewLabel)
        }
    }
    
    // 乘机人
    class SubPassengerView: UIView {
        
        public var subUser:OrderAccount?
        
        override func layoutSubviews() {
            guard let account = subUser else {
                return
            }
            // 下划线
            let line = UIView(frame: CGRect(x: MARGIN, y: 0, width: frame.width - MARGIN * 2, height: 1))
            line.backgroundColor = COLOR_LINE
            addSubview(line)
            //  乘机人文本
            let passenger = UILabel(title: "乘机人", color: COLOR_NICE, size: 15)
            passenger.frame = CGRect(x: MARGIN, y: line.frame.maxY + MARGIN25, width: MARGIN100, height: MARGIN20)
            addSubview(passenger)
            // 姓名
            let usernameY = passenger.frame.maxY + MARGIN5
            let username = UILabel(title: "", color: COLOR_NICE, size: 15)
            username.text = account.username
            username.frame = CGRect(x: MARGIN, y: usernameY, width: MARGIN50, height: MARGIN40)
            addSubview(username)
            // 身份证号
            let idnumber = UILabel(title: "", color: UIColor.gray, size: 14,align: .right)
            idnumber.text = "身份证/居民居住证 \(account.idnumber ?? "0")"
            let idnumberX = frame.width - username.frame.maxX - MARGIN
            idnumber.frame = CGRect(x: username.frame.maxX, y: usernameY, width: idnumberX, height: MARGIN40)
            addSubview(idnumber)
        }
    }
    
    // 联系人
    class SubContactView: UIView {
        
        public var subPassenger:OrderAccount?
        
        override func layoutSubviews() {
            guard let account = subPassenger else {
                return
            }
            // 联系人文本
            let contact = UILabel(title: "联系人", color: COLOR_NICE, size: 15)
            contact.frame = CGRect(x: MARGIN, y: MARGIN10, width: MARGIN100, height: MARGIN40)
            addSubview(contact)
            // 联系人
            let username = UILabel(title: "", color: UIColor.gray, size: 14,align: .right)
            username.text = account.passenger
            let usernameW = frame.width - contact.frame.maxX - MARGIN
            username.frame = CGRect(x: contact.frame.maxX, y: MARGIN10, width: usernameW, height: MARGIN40)
            addSubview(username)
            // 电话文本
            let mobileView = UILabel(title: "手机号", color: COLOR_NICE, size: 15)
            let mobileY = contact.frame.maxY
            mobileView.frame = CGRect(x: MARGIN, y: mobileY, width: MARGIN100, height: MARGIN40)
            addSubview(mobileView)
            // 电话
            let mobileLabel = UILabel(title: "", color: UIColor.gray, size: 14,align: .right)
            mobileLabel.text = account.mobile
            mobileLabel.frame = CGRect(x: contact.frame.maxX, y: mobileY, width: usernameW, height: MARGIN40)
            addSubview(mobileLabel)
        }
    }
    
    // 座位号
    class SubSeatNumberView: UIView {
        
        public var subOrderInfo: OrderInfo?
        
        override func layoutSubviews() {
            guard let ticket = subOrderInfo else {
                return
            }
            // 座位号文本
            let contact = UILabel(title: "座位号", color: COLOR_NICE, size: 15)
            contact.frame = CGRect(x: MARGIN, y: 0, width: MARGIN100, height: frame.height)
            addSubview(contact)
            // 座位号
            let username = UILabel(title: "", color: UIColor.gray, size: 14,align: .right)
            username.text = ticket.seatnumber ?? "自动分配"
            let usernameW = frame.width - contact.frame.maxX - MARGIN
            username.frame = CGRect(x: contact.frame.maxX, y: 0, width: usernameW, height: frame.height)
            addSubview(username)
        }
    }
    
    // 机票信息
    class SubTicketInfoView: UIView {
        
        public var subTicketInfoOrderInfo: OrderInfo?
        
        override func layoutSubviews() {
            guard let ticket = subTicketInfoOrderInfo else {
                return
            }
            // 日期
            let symbol = UILabel()
            symbol.textColor = COLOR_NICE
            symbol.text = AirLineUtil.shareInstance.getConvertDate(time: ticket.departdate!, isYear: true)
            symbol.font = UIFont.systemFont(ofSize: 13)
            symbol.frame = CGRect(x: MARGIN20, y: MARGIN , width: MARGIN140, height: MARGIN)
            addSubview(symbol)
            // 注意
            let cabinname = UILabel()
            cabinname.textAlignment = .right
            cabinname.text = "*航班起降时间均为当地时间"
            cabinname.font = UIFont.systemFont(ofSize: 12)
            cabinname.textColor = Palette.colorFromRGB(253, green: 123, blue: 110)
            cabinname.frame = CGRect(x: frame.width - 170 - MARGIN20, y: MARGIN, width: 170, height: MARGIN)
            addSubview(cabinname)
            // 出发时间
            let startTime = UILabel()
            startTime.text = ticket.departtime
            startTime.textColor = UIColor.black
            let startTimeY = symbol.frame.maxY + MARGIN20
            startTime.font = UIFont.boldSystemFont(ofSize: 25)
            startTime.frame = CGRect(x: MARGIN20, y:  startTimeY, width: MARGIN100, height: MARGIN30)
            addSubview(startTime)
            // 到达时间
            let stopTime = UILabel()
            stopTime.text = ticket.arrivaltime
            stopTime.textAlignment = .right
            stopTime.textColor = UIColor.black
            stopTime.font = UIFont.boldSystemFont(ofSize: 25)
            stopTime.frame = CGRect(x: frame.width - MARGIN120, y: startTimeY , width: MARGIN100, height: MARGIN30)
            addSubview(stopTime)
            // 箭头图片
            let arrowPic = UIImageView(image: UIImage(named: "checi_arrow"))
            arrowPic.frame.origin = CGPoint(x: center.x - arrowPic.frame.width * 0.5, y:startTimeY + MARGIN25 )
            addSubview(arrowPic)
            // 飞行时长
            let miletime = UILabel()
            miletime.text = ticket.miletime
            miletime.textColor = UIColor.gray
            let mileTextWidth = miletime.text!.boundingRect(w: MARGIN50, h: MARGIN, fontSize: 13).width * 0.5
            let offset = arrowPic.bounds.size.width * 0.5 - mileTextWidth
            miletime.font = UIFont.systemFont(ofSize: 13)
            let miletimeX = center.x - arrowPic.frame.width * 0.5 + offset
            miletime.frame = CGRect(x: miletimeX, y: arrowPic.frame.maxY - 18, width: MARGIN50, height: MARGIN)
            addSubview(miletime)
            // 出发航站楼
            let departTime = UILabel()
            departTime.textColor = COLOR_NICE
            departTime.text = "\(ticket.city!)机场 \(ticket.departterminal!)"
            departTime.font = UIFont.systemFont(ofSize: 13)
            departTime.frame = CGRect(x: MARGIN20, y: startTime.frame.maxY + MARGIN10 , width: MARGIN140, height: MARGIN)
            addSubview(departTime)
            // 到达航站楼
            let arrivalTime = UILabel()
            arrivalTime.textAlignment = .right
            arrivalTime.textColor = COLOR_NICE
            arrivalTime.text = "\(ticket.endcity!)机场 \(ticket.arrivalterminal!)"
            arrivalTime.font = UIFont.systemFont(ofSize: 13)
            let arrivalTimeX = frame.width - MARGIN140 - MARGIN20
            arrivalTime.frame = CGRect(x: arrivalTimeX, y: startTime.frame.maxY + MARGIN10 , width: MARGIN140, height: MARGIN)
            addSubview(arrivalTime)
            // 航班信息
            let airLineInfo = UILabel()
            airLineInfo.textColor = COLOR_NICE
            airLineInfo.text = "\(ticket.flightnumberid!) | \(ticket.cabinname!)  | \(ticket.craft!) | \(ticket.food!) | 里程:\(Int(ticket.mileage))KM"
            airLineInfo.font = UIFont.systemFont(ofSize: 12)
            let airLineInfoY = departTime.frame.maxY + MARGIN20
            airLineInfo.frame = CGRect(x: MARGIN20, y: airLineInfoY, width: SCREEN_WIDTH - MARGIN40, height: MARGIN)
            addSubview(airLineInfo)
        }
    }
    
    // 头部
    class SubTopView: UIView {
        
        private lazy var statusPic = UIImageView(image: UIImage(named: "status0"))
        
        public var subTopOrderInfo: OrderInfo?
        
        override func layoutSubviews() {
            guard let ticket = subTopOrderInfo else {
                return
            }
            // 头部 图片
            statusPic.image = UIImage(named: "status\(ticket.orderstatus)")
            statusPic.frame.origin = CGPoint(x: MARGIN, y: (frame.height - statusPic.bounds.height) * 0.5)
            addSubview(statusPic)
            // 头部 状态
            let topStatus = UILabel(title: "", color: UIColor.black)
            topStatus.text = ticket.orderstatus == 0 ? "待出行" : "已结束"
            topStatus.font = UIFont.boldSystemFont(ofSize: 25)
            topStatus.frame = CGRect(x: statusPic.frame.maxX + MARGIN10, y: 0, width: MARGIN100, height: MARGIN100)
            addSubview(topStatus)
            // 头部 票价符号
            let topSymbol = UILabel(title: "¥", color: UIColor.black,size: 13,align: .right)
            let topSymbolX = frame.width - MARGIN70 - MARGIN
            topSymbol.frame = CGRect(x: topSymbolX, y: 0, width: MARGIN20, height: MARGIN100)
            addSubview(topSymbol)
            // 改签票
            let changeTicket = UILabel(title: "改签票", color: UIColor.gray, size: 11, align: .center)
            changeTicket.layer.cornerRadius = 3
            changeTicket.layer.borderWidth = 1
            changeTicket.layer.borderColor = UIColor.gray.cgColor
            let changeY = MARGIN60
            changeTicket.frame = CGRect(x: topSymbolX + MARGIN10, y: changeY, width: MARGIN50, height: MARGIN20)
            if ticket.changorder == 1 {
                addSubview(changeTicket)
            }
            // 头部 具体票价
            let topTicketPrice = UILabel(title: "", color: UIColor.black)
            let topTicketPriceX = topSymbol.frame.maxX
            topTicketPrice.font = UIFont.boldSystemFont(ofSize: 20)
            topTicketPrice.text = String(describing: ticket.ticketprice)
            topTicketPrice.frame = CGRect(x: topTicketPriceX, y: 0, width: MARGIN50, height: 95)
            addSubview(topTicketPrice)
        }
    }
}
